package com.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.mvc.entity.ShopCar;
import com.mvc.model.Gwc;
import com.mvc.model.Result;
import com.mvc.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName ShopCarController
 * @Description TODO
 * @Author king
 * @Date 2021/11/27 21:11
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/shop_car", produces = "text/html;charset=UTF-8")
public class ShopCarController {
    @Autowired
    private ShopCarService shopCarService;
    @RequestMapping(value = "/list.do")
    @ResponseBody
    public String listSearch(@RequestParam Integer page, @RequestParam Integer limit){
        Integer startRow = (page - 1) * limit;
        List<ShopCar> list = shopCarService.listSearch(startRow, limit);
        Integer count = shopCarService.getCount();
        Result result = new Result();
        result.setCode(0);
        result.setData(list);
        result.setCount(count);
        String json = JSON.toJSONString(result);
        return json;
    }
    @RequestMapping(value = "/add.do")
    @ResponseBody
    public String add(ShopCar ShopCar){
        Integer n = shopCarService.insert(ShopCar);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "/update.do")
    @ResponseBody
    public String update(ShopCar ShopCar){
        Integer n = shopCarService.update(ShopCar);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "/del.do")
    @ResponseBody
    public String del(@RequestParam Integer id){
        Integer n = shopCarService.del(id);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;
    }


        @RequestMapping(value = "/Gsearch.do")
        @ResponseBody
        public String search(Gwc gwc){
            List<Gwc> list = shopCarService.search(gwc);
            Result result = new Result();
            result.setCode(0);
            result.setData(list);
            String json = JSON.toJSONString(result);
            return json;
        }

        @RequestMapping(value = "/Gupdate.do")
        @ResponseBody
        public String Gupdate(Gwc gwc){
            Integer n = shopCarService.Gupdate(gwc);
            Result result = new Result();
            if (n == 1){
                result.setCode(0);
                result.setMsg("成功");
            }else {
                result.setCode(1);
                result.setMsg("失败");
            }
            String json = JSON.toJSONString(result);
            return json;
        }

        @RequestMapping(value = "/Gdel.do")
        @ResponseBody
        public String Gdel(@RequestParam Integer id) {
            Integer n = shopCarService.Gdel(id);
            Result result = new Result();
            if (n == 1) {
                result.setCode(0);
                result.setMsg("成功");
            } else {
                result.setCode(1);
                result.setMsg("失败");
            }
            String json = JSON.toJSONString(result);
            return json;
        }
}
